<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Authgroup as AuthModel;
use app\admin\model\Guanli;

class Authgroup extends Controller
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->Authgroup=new AuthModel();
    }
    //查询
    public function index(){
        $jiao= $this->Authgroup->select();
        return view('index',['jiao'=>$jiao]);
    }

    //添加
    public function jiaoseadd(){
        //判断是否为post提交
        if(request()->isAjax()){
            $data=[
              'jname'=>input('jname'),
              'describe'=>input('describe')
            ];
            $re=$this->Authgroup->insert($data);
            return $re;
        }else{
            $jiao = $this->Authgroup->select();
            return view('jiaoseadd',['jiao'=>$jiao]);
        }
    }

    //修改
    public function update(){
        $id=input('id');
        //halt($id);
        $rs=db('auth_group')
            ->where('jc_auth_group.id',$id)
            ->find();
        //halt($rs);
        if(request()->isAjax()){
            $id=input('id');
            $rs=db('auth_group')
                ->where('id',$id)
                ->update(['jname'=>input('jname'),'describe'=>input('describe')]);
            if($rs){
                return array('code'=>1,'msg'=>"修改成功");
            }else{
                return array('code'=>0,'msg'=>"修改失败");
            }
        }else{

            return view('',['rs'=>$rs,'id'=>$id]);
        }
    }
    //设置权限
    public function setquan(){
        $id=input('id');
        //查询角色
        $res=db('auth_group')->where('id',$id)->find();
        //将rules分割为数组
        $rules=$res['rules'];
        $arr=array_filter(explode(',',$rules));
        //查出所有被选中的权限
        for($i=0;$i<count($arr);$i++){
            $data[]=db('auth_rule')
                ->where('jc_auth_rule.id',$arr[$i])
                ->select();
        }
        //查出所有权限
        $result=db('auth_rule')->where('jc_auth_rule.id','not in',$rules)->select();
        //halt($result);
        if(request()->isPost()){
            $id=input('hid');
            $value=input('post.access/a');//halt($value);
            if($value=='')
            {
              return array('code'=>0,'msg'=>'权限不能为空哦~');
            }
            $rules=implode(',',$value);
            $edit=db('auth_group')->where('id',$id)->update(['rules'=>$rules]);
            if($edit){
                return array('code'=>1,'msg'=>"设置成功");
            }
            else{
                return array('code'=>0,'msg'=>"设置失败");
            }
        }else{
            return view('',['res'=>$res,'data'=>$data,'result'=>$result]);
        }
    }

    //权限的禁用
    public function jin()
    {
            $where['id']=input('id');
            $data=['jc_auth_group.status'=>0];
            $rs=$this->Authgroup->updateByjin($where,$data);
            return $rs;
    }
    //权限的启用
    public function qi()
    {
        $where['id']=input('id');
        $data=['jc_auth_group.status'=>1];
        $rs=$this->Authgroup->updateByjin($where,$data);
        return $rs;
    }

}